//
//  JRWelcomeViewController.swift
//  myWeibo
//
//  Created by 樊健荣 on 16/7/21.
//  Copyright © 2016年 樊健荣. All rights reserved.
//

import UIKit
import SnapKit
class JRWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

//        self.view.backgroundColor = UIColor.whiteColor()
        
        setupUI()
        
    }
    //在这个方法里面实行动画
    override func viewDidAppear(animated: Bool) {
        startAniamtion()
    }
    
    ///开始动画
    private func startAniamtion() {
        iconImageV.snp_updateConstraints { (make) -> Void in
            
            let padding = UIScreen.mainScreen().bounds.height - 160
            
            UIView.animateWithDuration(1, delay: 0.5, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
                make.bottom.equalTo(self.view).offset(-padding)
                }, completion: { (_) -> Void in
                    
                    UIView.animateWithDuration(0.25, animations: { () -> Void in
                        self.welcomeLabel.alpha = 1
                        }, completion: { (_) -> Void in
                            AppDelegate.switchRootViewController(JRMainTabBarController())
                    })
                    
            })
            
            
            
        }
    }
    
    
    ///设置控件
    private func setupUI() {
        
        //添加控件
        self.view.addSubview(bgImageV)
        self.view.addSubview(iconImageV)
        self.view.addSubview(welcomeLabel)
        
        //添加约束
        //背景
        bgImageV.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.view)
        }
        //头像
        iconImageV.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(self.view).offset(-160)
            make.centerX.equalTo(self.view)
        }
        
        //欢迎
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.iconImageV.snp_bottom).offset(16)
            make.centerX.equalTo(self.view)
        }
        
        
    }

    // MARK: - 懒加载控件
    ///背景图片
    lazy var bgImageV: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    ///头像
    private lazy var iconImageV: UIImageView = {
        
        let iconV = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        iconV.layer.cornerRadius = 42.5
        iconV.clipsToBounds = true
        
        return iconV
    }()
    
    ///欢迎
    private lazy var welcomeLabel: UILabel = {
        
        let welcome = UILabel()
        welcome.text = "欢迎归来"
        welcome.font = UIFont.systemFontOfSize(18)
        welcome.alpha = 0
        welcome.sizeToFit()
        return welcome
    }()
    


}
